package br.com.daniele.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import br.com.daniele.services.PdfServices;

@Controller
@RequestMapping(value = "/upload")
public class PdfController {
	
	@Autowired
	private PdfServices pdfServices;
	
	/*@Autowired
	private FileValidator fileValidator;*/
	
	//private String property = System.getProperty("user.dir");
	
	@RequestMapping(value = "/toUpload")	
	public String toUpload(){
		return "pages/upload/uploadFiles";
	}
	
	
	@RequestMapping(value = "/toDownload")
	public ModelAndView toDownload(){
		
		ModelAndView view = new ModelAndView("pages/download/downloadFile");
		view.addObject("numeroVariavelArquivo", PdfServices.fileNumberVariable);
		
		PdfServices.fileNumberVariable++;
		
		return view;
	}
	
	
	
	@RequestMapping(value = "/uploadFile")
	public String upload( @ModelAttribute(value = "uploadedFile") UploadFile uploadFile, BindingResult result ){
		
		pdfServices.uploadXMLToParsePdf(uploadFile, result);
		System.out.println("GERANDO DANFE...");
		
		return "pages/upload/uploadFiles";
		
	}	
	
	
	@RequestMapping(value = "/fileNumberVariable")
	public @ResponseBody int getFileNumberVariable(){
		
		int fileNumberVariable = PdfServices.fileNumberVariable;
		
		PdfServices.fileNumberVariable++;
		
		return fileNumberVariable;
	}
	
	
	@RequestMapping(value = "/true")
	public @ResponseBody int existFile(@RequestParam String filename){
		return 0;
	}
	

	/*public static void main(String[] args) throws IOException {
		
		for(Object key: System.getProperties().keySet()){
			System.out.println("chave: "+ key + " ==== valor: " + System.getProperty(String.valueOf(key)));
		}	
				
	}*/
	

}



